<?php
class AliyunOSSFirstIMG extends ZBlogPHP {

	function GetImgList($type,$sql){
		global $obj,$bucket;

		$array=null;
		$list=array();
		$array=$this->db->Query($sql);
		if(!isset($array)){return array();}
		foreach ($array as $a) {
			$l=new $type();
			$l->LoadInfoByAssoc($a);

			$randnum=mt_rand(1,20);
			$pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
			$content = $l->Content;
			preg_match_all($pattern,$content,$matchContent);

			if(isset($matchContent[1][0])) {
				$temp=$matchContent[1][0];
			}else{
				$temp=$this->host . "zb_users/plugin/AliyunOSS/noimg/" . $randnum . ".jpg";
			}
			if(substr($temp,0,strlen(OSS_OBJECT_URL))==OSS_OBJECT_URL) {
				$ext = strtolower(strrchr($temp,"."));
				$thumbSrc = substr($temp,0,-strlen($ext)).'_mini'.$ext;
//				$thumbSrcSize = getimagesize($thumbSrc);
				$object = substr($thumbSrc,strlen(OSS_OBJECT_URL));	//构造云文件名
				$file_path = $this->usersdir . 'upload/tmp' . rand(10000,99999) . time() . $ext;

				$is_object_exist = $obj->is_object_exist($bucket, $object);
				$thumbSrcStatus = $is_object_exist->status;

//				print_r($is_object_exist);

				if($thumbSrcStatus != 200) {

					$getpic=AliyunOSS_Img2thumb($temp,$file_path,THUMB_WIDTH,THUMB_HEIGHT,THUMB_CUT,THUMB_PROPORTION);

					if(is_file($file_path)) {
						$response = $obj->upload_file_by_file($bucket,$object,$file_path);//将本地文件上传到云
						unlink($file_path);
					}
				}

				$l->Img=$thumbSrc;
			}else{
				$l->Img=$temp;
			}
			$list[]=$l;
		}
		return $list;
	}

	function GetArticleList($select=null,$where=null,$order=null,$limit=null,$option=null,$readtags=true){

		if(empty($select)){$select = array('*');}
		if(empty($where)){$where = array();}
		if(is_array($where))array_unshift($where,array('=','log_Type','0'));
		$sql = $this->db->sql->Select($this->table['Post'],$select,$where,$order,$limit,$option);
		$array = $this->GetImgList('Post',$sql);

		if($readtags){
			$tagstring = '';
			foreach ($array as $a) {
				$tagstring .= $a->Tag;
				$this->posts[$a->ID]=$a;
			}
			$this->LoadTagsByIDString($tagstring);
		}

		return $array;

	}

	function GetPostList($select=null,$where=null,$order=null,$limit=null,$option=null){

		if(empty($select)){$select = array('*');}
		if(empty($where)){$where = array();}
		$sql = $this->db->sql->Select($this->table['Post'],$select,$where,$order,$limit,$option);
		return $this->GetImgList('Post',$sql);

	}
}

$zbp=new AliyunOSSFirstIMG();
$zbp->Initialize();

